package com.zx.crowdfunding.mvc.config;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.zx.crowdfunding.global.CrowdFundingGlobal;
import com.zx.crowdfunding.service.api.AuthService;

/**
 * 服务器启动监听器
 * @author 郑雪
 * @date 2021-11-20
 */
@Component
public class ServerStartUpListener implements ServletContextListener {
	
	/**
	 * 日志
	 */
	protected final Log logger = LogFactory.getLog(getClass());
	
	/**
	 * WEB应用对象初始化时被监听到
	 */
	@Override
	public void contextInitialized(ServletContextEvent sce) {
		// 获取Servlet上下文
		ServletContext servletContext = sce.getServletContext();
		// 获取Spring的上下文
		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		logger.info("项目启动完成，开始加载代码权限...");
		// 权限Service接口
		AuthService authService = webApplicationContext.getBean(AuthService.class);
    	// 更新全局静态变量，为了后续代码开发时使用。同时放入Application域中，方便页面中随时获取。
    	authService.updateGlobalCodeAuth(servletContext);
    	logger.info("加载代码权限 ：" + CrowdFundingGlobal.CODE_AUTH);
    	logger.info("加载代码权限完成！！！");
	}
	
	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub

	}

}
